
<?
	  include("/inc/header.html") {}
    ?>    

      <div class="panel panel">          
                                <div class="panel-body pan">
                                    
                                        <div class="form-body pal">
                                            <div class="row">
                                                <div class="col-md-6">
                                                    <div class="form-group">
                                                     <label for="inputUsername" >中文名称 <span class='require'>*</span> </label>
                                                        <div class="input-icon right">
                                                            <input id="nameZh" type="text" class="form-control" onchange="pinYin(this.value)" />
                                                        </div>
                                                    </div>
                                                </div>
                                                <div class="col-md-6">
                                                    <div class="form-group">
                                                     <label for="inputUsername" >英文名称 <span class='require'>*</span> </label>
                                                        <div class="input-icon right">
                                                          <input id="nameEn" type="text" class="form-control" onchange="voluation(this.value,0);" />
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                            <div class="row">
                                                <div class="col-md-6">
                                                    <div class="form-group">
                                                        <div class="input-icon right">   
                                                         <label for="inputUsername" >字段名称 <span class='require'>*</span> </label>
                                                            <input id="field" type="text" class="form-control" readonly="readonly"/>
                                                        </div>
                                                    </div>
                                                </div>
                                                <div class="col-md-6">
                                                    <div class="form-group">
                                                        <div class="input-icon right">
                                                         <label for="inputUsername" >排序 <span class='require'>*</span> </label>
                                                            <input id="sort" type="text" class="form-control"/>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                            <hr/>
                                            <div class="row">
                                                <div class="col-md-6">
                                                    <div class="form-group"><label for="inputUsername" >公共字段  </label>
                                                        <select  id="commonObjectPropertyId"  class="form-control" onchange="gradeChange()"> 
                                                        <option value="00">---请选择---</option>    
                                                        </select>
                                                    </div>
                                                </div>
                                                
                                                <div class="col-md-6">
                                                    <div class="form-group"><label for="inputUsername" >关联类<span class='require'>*</span> </label>
                                                        <select id="relevanceClassesId" class="form-control" onchange="gradeFields()">
                                                        </select>
                                                    </div>
                                                </div>
                                            <div class="col-md-6">
                                            <div class="form-group"><label for="inputUsername" >关联类字段 <span class='require'>*</span> </label>
								   <div class="input-group input-group-sm mbs" >
									 <input id="Fieldsname" type="text"   class="form-control" disabled="true"  />
									 <span class="input-group-btn"><button onclick="saveFields()" type="button" data-toggle="dropdown" class="btn btn-success dropdown-toggle">选择字段..</button></span>
							 <input id="relevanceClassFields" type="hidden"  class="form-control" /> 
						
								</div>
								</div></div>
								</div>
                                                
                                                      <hr/>
                                              <div class="row">
                                                <div class="col-md-6">
                                                    <div class="form-group"><label for="inputUsername" >是否多值 <span class='require'>*</span> </label>
                                                        <select  id="multiValued"  class="form-control">
                                                             <option value="0">false</option>
                                                             <option value="1">true</option>
                                                        </select>    
                                                    </div>                                                   
                                                </div>
                                                <div class="col-md-6">
                                                    <div class="form-group"><label for="inputUsername" >是否必填 <span class='require'>*</span> </label>
                                                        <select  id="required"  class="form-control">
                                                             <option value="0">false</option>
                                                             <option value="1">true</option>
                                                        </select>    
                                                    </div>                                                   
                                                </div>
                                                 <div class="col-md-6">
                                                    <div class="form-group"><label for="inputUsername" >是否内容存储 <span class='require'>*</span> </label>
                                                        <select  id="stored"  class="form-control">
                                                             <option value="0">false</option>
                                                             <option value="1" selected>true</option>
                                                        </select>    
                                                    </div>                                                   
                                                </div>
                                                 <div class="col-md-6">
                                                    <div class="form-group"><label for="inputUsername" >是否建立索引<span class='require'>*</span> </label>
                                                        <select  id="indexed"  class="form-control">
                                                             <option value="0">false</option>
                                                             <option value="1" selected>true</option>
                                                        </select>    
                                                    </div>                                                   
                                                </div>
                                                 <div class="col-md-6">
                                                    <div class="form-group"><label for="inputUsername" >是否用作特性 <span class='require'>*</span> </label>
                                                        <select  id="termVectors"  class="form-control">
                                                             <option value="0">false</option>
                                                             <option value="1">true</option>
                                                        </select>    
                                                    </div>                                                   
                                                </div>
                                                <div id="showSeparator" class="col-md-6">
                                                    <div class="form-group"><label for="inputUsername" >分隔符</label>
                                                       <div class="input-icon right">
                                                          <input id="separator"  type="text" class="form-control"/>
                                                        </div>    
                                                     
                                                    </div>                                                   
                                                </div>
                                            </div>
                                         <hr/>
                                           <div class="row">
                                            <div class="col-md-6">
                                                    <div class="form-group"><label for="inputUsername" >是否前台列表页显示 <span class='require'>*</span> </label>
                                                        <select  id="webListShow"  class="form-control">
                                                             <option value="1">true</option>
                                                             <option value="0">false</option>
                                                        </select>    
                                                    </div>                                                   
                                                </div>   
                                                 <div class="col-md-6">
                                                    <div class="form-group"><label for="inputUsername" >是否前台详情页显示 <span class='require'>*</span> </label>
                                                        <select  id="webDetailShow"  class="form-control">
                                                             <option value="1">true</option>
                                                             <option value="0">false</option>
                                                        </select>    
                                                    </div>                                                   
                                                </div>   
                                               <div class="col-md-6">
                                                    <div class="form-group"><label for="inputUsername" >是否后台列表页显示 <span class='require'>*</span> </label>
                                                        <select  id="adminListShow"  class="form-control">
                                                             <option value="1">true</option>
                                                             <option value="0">false</option>
                                                        </select>    
                                                    </div>                                                   
                                                </div>
                                                 <div class="col-md-6">
                                                    <div class="form-group"><label for="inputUsername" >是否后台详情页显示 <span class='require'>*</span> </label>
                                                        <select  id="adminDetailShow"  class="form-control">
                                                             <option value="1">true</option>
                                                             <option value="0">false</option>
                                                        </select>    
                                                    </div>                                                   
                                                </div>
                                                                              
                                          
                                           </div>
                                        </div>
                                </div>
                            </div>   
               
 <script>
var classid = window.parent.getclassid();
var objectOrder = window.parent.getobjectOrder();
var objectPropertyid = window.parent.getobjectPropertyid();
 $(function(){
		if($("#multiValued").val()==0){
			$("#showSeparator").hide();
		}
	 $("#sort").val(objectOrder);
	 	xiala();
		
	}); 
	$(document).ready(function () { 
		  $("#multiValued").bind("change",function(){ 
		    if($(this).val()==1){
		   		$("#showSeparator").show();
		    }else{
		    	$("#showSeparator").hide();
		    } 
		  }); 
		});
var selectname;
  function getselectname()
  {
  return selectname;
  }
function gradeFields()
 {
    var objS = document.getElementById("relevanceClassesId");
	var relevanceClassesId = objS.options[objS.selectedIndex].value;
	return relevanceClassesId;
 }
function xiala() { 
 $.ajax({  
                type : "post",  
                url : "./KnowledgeCommonObjectall.do",  
                data : {},
                dataType : "json",   
                success : function(data) { 
                   $(data).each(function(n){ 
                      $("<option/>").html(data[n].nameZh).val(data[n].id)
                            .appendTo("#commonObjectPropertyId");
                              
                            });  
                     xialaclass();
                          
                },
	            error : function(data) {
                   alert("请求失败");
		        }
            }); 
          
         }
function xialaclass() { 
 $.ajax({  
                type : "post",  
                url : "./KnowledgeclassListall.do",  
                data : {},
                dataType : "json",   
                success : function(data) { 
                   $(data).each(function(n){ 
                 /*   if(classid!=data[n].id)
                   { */
                      $("<option/>").html(data[n].name).val(data[n].id)
                            .appendTo("#relevanceClassesId");
                              
                           /* }  */}); 
                      if(objectPropertyid!=0){
							edit();
						} 
                      
                },
	            error : function(data) {
                   alert("请求失败");
		        }
            }); 
          
         }
 
function save() {   
       if ($.trim($("#sort").val()).length < 1) {
				alert('排序不能为空！');
				return null;
			}
			if (isNaN($("#sort").val())) {
				alert("排序必须是数字");
				return null;
			}
			 if ($.trim($("#nameEn").val()).length < 1) {
				alert('英文名不能为空！');
				return null;
			} 
			 if($.trim($("#nameEn").val()).substring(0, 3) != 'op_'){
				alert('英文名必须以op_开头！');
				return null;
			} 
			if ($.trim($("#nameZh").val()).length < 1) {
				alert('中文名不能为空！');
				return null;
			}
			if ($.trim($("#relevanceClassFields").val()).length < 1) {
				alert('关联字段不能为空！');
				return null;
			}
      
   
    $.ajax({  
                type : "post",  
                url : "./KnowledgeObjectPropertysave.do",  
                data : {
                "id":objectPropertyid,
                "classid":classid,
                "nameEn":$("#nameEn").val(),
                "nameZh":$("#nameZh").val(),
                "sort":$("#sort").val(),
	            "multiValued":$("#multiValued").val(),
		        "required":$("#required").val(),
		        "stored":$("#stored").val(),
		       "indexed":$("#indexed").val(),
		       "termVectors":$("#termVectors").val(),
		       "webListShow":$("#webListShow").val(),
		       "webDetailShow":$("#webDetailShow").val(),
		       "adminListShow":$("#adminListShow").val(),
		       "adminDetailShow":$("#adminDetailShow").val(),
                "field":$("#field").val(),
                "relevanceClassFields":$("#relevanceClassFields").val(),
                "relevanceClassesId":$("#relevanceClassesId").val(),
                "commonObjectPropertyId":$("#commonObjectPropertyId").val(),
                "separator" : $("#separator").val()},
                dataType : "json", 
                async:false,  
                success : function(data) { 
              var index = parent.layer.getFrameIndex(window.name); 
            parent.layer.msg('添加或编辑成功');
            window.parent.xianshiclass();
			parent.layer.close(index);
           
                },
	            error : function(data) {
                   alert("请求失败");
		        }
            });

          
    }  
   function edit(){
  
    $.getJSON('./KnowledgeObjectPropertyedit.do', {
        id: objectPropertyid //向服务端传的参数，此处只是演示
    }, function(res){
    	//alert(JSON.stringify(res));
	    for(var k=0; k<res.length;k++){
	        //此处仅仅是为了演示变化的内容
	         $("#nameEn").val(res[k].text.nameEn);
	         document.getElementById('nameEn').disabled = true;
	          $("#nameZh").val(res[k].text.nameZh);
	         $("#sort").val(res[k].text.sort);
	         $("#field").val(res[k].text.field);
	         var Fieldsname="";
	         var relevanceClassFields="";
	         if(res[k].dataProperty!=null) {
	          for(var i=0;i<res[k].dataProperty.length;i++){
		          Fieldsname+=res[k].dataProperty[i].nameZh+",";
		          relevanceClassFields+=res[k].dataProperty[i].id+",";
	          }
	         }
	         
	        Fieldsname=Fieldsname.substring(0, Fieldsname.length-1);
			relevanceClassFields=relevanceClassFields.substring(0, relevanceClassFields.length-1);
			selectname=relevanceClassFields;
	         $("#Fieldsname").val(Fieldsname);
	         $("#relevanceClassFields").val(relevanceClassFields);
	         
	         selected( "relevanceClassesId",res[k].text.relevanceClassesId);
	         if(res[k].commonObjectPropertyId!=0){
	          document.getElementById('nameEn').disabled = true;
		        document.getElementById('multiValued').disabled = true;
				document.getElementById('required').disabled = true;
				document.getElementById('stored').disabled = true;
				document.getElementById('indexed').disabled = true;
				document.getElementById('termVectors').disabled = true;
		        selected( "commonObjectPropertyId",res[k].text.commonObjectPropertyId);
		        document.getElementById('commonObjectPropertyId').disabled = true;
	        }
	        selected("multiValued", res[k].text.multiValued);
			selected("required", res[k].text.required);
			selected("stored", res[k].text.stored);
			selected("indexed", res[k].text.indexed);
			selected("termVectors", res[k].text.termVectors);
			selected("webListShow", res[k].text.webListShow);
			selected("webDetailShow", res[k].text.webDetailShow);
			selected("adminListShow", res[k].text.adminListShow);
			selected("adminDetailShow",res[k].text.adminDetailShow);
		}							
	});
        
 
};
 function gradeChange()
 {
   var objS = document.getElementById("commonObjectPropertyId");
			var commonObjectPropertyid = objS.options[objS.selectedIndex].value;
			if ("00" == commonObjectPropertyid) {
				document.getElementById('multiValued').disabled = false;
				document.getElementById('required').disabled = false;
				document.getElementById('stored').disabled = false;
				document.getElementById('indexed').disabled = false;
				document.getElementById('termVectors').disabled = false;
			} else {
				$.getJSON('./KnowledgeCommonObjectedit.do',
								{
									id : commonObjectPropertyid
								},
								function(res) {
								    $("#field").val(res[0].field);	
								    $("#nameEn").val(res[0].nameEn);
									selected("multiValued", res[0].multiValued);
									selected("required", res[0].required);
									selected("stored", res[0].stored);
									selected("indexed", res[0].indexed);
									selected("termVectors", res[0].termVectors);
									document.getElementById('multiValued').disabled = true;
									document.getElementById('nameEn').disabled = true;
									document.getElementById('required').disabled = true;
									document.getElementById('stored').disabled = true;
									document.getElementById('indexed').disabled = true;
									document.getElementById('termVectors').disabled = true;
									voluation(res[0].nameEn,1);		
								});
						
			}
			
 
 }
 function saveFields()
 {
 parent.layer.open({
			type : 2,
			area :[ '800px', '500px' ],
			fix : false, //不固定
			scrollbar : false,
			shadeClose : true,
			//move : false,
			title:"选择字段",
			zIndex: layer.zIndex,
			skin : 'layui-layer-rim', //加上边框
			btn : [ '保存 ', '关闭' ],
			content :["knowledge-DataProperty-class-Fields.html?winName="+window.name, 'no'],
			yes : function(index,layer) {			
			  var iframeWin = parent.window[layer.find('iframe')[0]['name']];
			  		iframeWin.save();
			  		
			},
			cancel : function(index) {
			}
		});
 }

 	//   默认单选函数
		function selected(selectname, selectid) {
			$("#" + selectname + " option").each(function() { //遍历全部option
				var v = $(this).val(); //获取option的内容
				if (v == selectid) {
					$(this).attr("selected", "selected");
				}
			});
		}
			 //赋值函数
  function voluation(nameen,kk){

      if ($.trim(nameen).length < 1) {
			alert('英文名不能为空！');
			return null;
		}
		if ($.trim(nameen).length > 32) {
			alert('英文名长度过长！');
			$("#nameEn").val("op_");
			$("#field").val("");
			return null;
		}
		if($.trim($("#nameEn").val()).substring(0, 3) == 'op_'){
			var reg = "^[a-z_]+$";     
	        var r = nameen.match(reg);
	        	if(r != null){
					$.ajax({
						type : "post",
						url : "./KnowledgeDataPropertyProving.do",
						data : {
						    "id":classid,
							"name" : nameen,
							"commonObjectPropertyId":$("#commonObjectPropertyId").val()
						},
						async : false,
						dataType : "json",
						success : function(data) {
							if(data.result==2){
								alert("表名不可用，已存在改公共字段");
								$("#nameEn").val("");
								if(kk==1){
									selected("commonObjectPropertyId","00");
									document.getElementById('multiValued').disabled = false;
									document.getElementById('required').disabled = false;
									document.getElementById('stored').disabled = false;
									document.getElementById('indexed').disabled = false;
									document.getElementById('termVectors').disabled = false;
									document.getElementById('nameEn').disabled = false;
								}
								
							}
							if(data.result==3){
								alert("表名不可用，不符合规则！");
								$("#nameEn").val("");
							}
							if(data.result==4){
								alert("表名不可用，必须为英文名！");
								$("#nameEn").val("");
								
							}
							if(data.result==5){
								alert("表名不可用，与公共字段冲突！");
								$("#nameEn").val("");
								
							}
							if(data.result==6){
								alert("表名不可用，已存在该表名！");
								$("#nameEn").val("");
							}
							  $("#field").val($("#nameEn").val());
						},
						error : function(data) {
							alert("请求失败");
						}
					});
				 }else{
		        	alert("命名不规范，命名必须全小写，如有多个需隔开请以英文下划线分割（例：op_word_name）");
					$("#nameEn").val("");
	        	} 
	        }else{
	        	alert('英文名必须以op_开头！');
	        }
		}
		//汉字转拼音
  function pinYin(pinyinName){
   var cop =$("#commonObjectPropertyId").val();
	  if(objectPropertyid == 0){
	  	if(cop == 00){
	  	var reg = "[A-Z]+";     
        var r = pinyinName.match(reg);
	        if(r == null){
				$.ajax({
					type : "post",
					url : "hanziToinitial.do",
					data : {
						"pinYin" : pinyinName,
					},
					async : false,
					dataType : "json",
					success : function(data) {
						if(data[0].length > 20){
							layer.msg('自动转换英文名长度过长!请手动输入！');
							$("#nameEn").val("op_");
							$("#field").val("");
						}else{
							$("#nameEn").val("op_"+data);
							$("#field").val("op_"+data);
						}
					},
					error : function(data) {
						alert("请求失败");
					}
				});
			}else{
				alert("英文名不可存在大写，请手动输入英文名");
				$("#nameEn").val("");
				$("#field").val("");
			}
			}
		}
	}
  
</script>